#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-3.0-or-later

build() {
    add_module "cdrom"
    add_module "loop"
    add_module "dm-snapshot"
    add_module "overlay"

    add_runscript

    add_binary blockdev
    add_binary dmsetup
    add_binary losetup
    add_binary openssl
    add_binary lsblk

    if command -v pv >/dev/null 2>&1; then
        add_binary pv
    else
        warning 'pv not found; falling back to cp for copy to RAM'
    fi

    add_udev_rule 60-cdrom_id.rules
    add_udev_rule 10-dm.rules
    add_udev_rule 95-dm-notify.rules
    add_udev_rule /usr/lib/initcpio/udev/11-dm-initramfs.rules

    if [[ -v LINGMOISO_GNUPG_FD ]]; then
        add_binary gpg
        add_dir '/gpg' 0700
        gpg --homedir "$BUILDROOT/gpg" --import <&"$LINGMOISO_GNUPG_FD"
    fi
    if [[ -v LINGMOISO_TLS_FD ]]; then
        cat <&"$LINGMOISO_TLS_FD" | add_file - '/codesign.crt' 644
    fi
    if [[ -v LINGMOISO_TLSCA_FD ]]; then
        cat <&"$LINGMOISO_TLSCA_FD" | add_file - '/codesign_CA.crt' 644
    fi
}
